def obj2dict(obj,keys=None):
    if not obj:
        return None

    # 判断是否是Query
    # 定义一个字典对象
    dictionary = {}
    if keys:
        dictionary = {key:getattr(obj,key) for key in keys}
    else:
        # 检索记录中的成员
        for field in [x for x in dir(obj) if
                      # 过滤属性
                      not x.startswith('_')
                      # 过滤掉方法属性
                      and hasattr(obj.__getattribute__(x), '__call__') == False
                      # 过滤掉不需要的属性
                      and x != 'metadata'
                      and x != 'query']:
            data = obj.__getattribute__(field)

            if hasattr(data, 'query'):
                data = obj2dict(data)
            try:
                dictionary[field] = data
            except TypeError:
                dictionary[field] = None
    # 返回字典对象
    return dictionary
